﻿using System;
using System.Net;
using System.Reflection;
using System.IO;
using System.Resources;
using System.Text;

namespace Sausage.Common.Pinyin
{
    /// <summary>
    /// 拼音转换器
    /// </summary>
    /// <remarks></remarks>
    /// <history>
    /// [zanezeng]               2010/06/10 12:00    创建
    /// </history>
    public static class PinYinConverter
    {
        #region ---Constructor

        /// <summary>
        /// 初始化类型<see cref="PinYinConverter"/>
        /// </summary>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2010/06/10 12:06    创建
        /// </history>
        static PinYinConverter()
        {
            //获得字符字典
            using (MemoryStream stream = new MemoryStream(Dictionary.CharDictionary))
            {
                //创建二进制读取器
                using (BinaryReader reader = new BinaryReader(stream))
                {
                    //读取字符字典
                    g_CharDictionary = CharDictionary.Load(reader);
                }
            }

            //读取拼音字典
            using (MemoryStream stream = new MemoryStream(Dictionary.PinyinDictionary))
            {
                //创建二进制读取器
                using (BinaryReader reader = new BinaryReader(stream))
                {
                    //读取字符字典
                    g_PinyinDictionary = PinyinDictionary.Load(reader);
                }
            }
        }

        #endregion

        #region ---Public Method

        /// <summary>
        /// 将指定的汉字字符串转换为拼音。
        /// </summary>
        /// <param name="source">要转换为拼音的汉字字符串。</param>
        /// <returns>将指定的汉字字符串转换出来的拼音。</returns>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2010/06/10 12:03    创建
        /// </history>
        public static string ConvertToPinYin(string source)
        {
            //用于保存结果
            StringBuilder builder = new StringBuilder();

            //循环处理字符
            foreach (char item in source.ToCharArray())
            {
                //获得字符单元
                CharUnit unit = g_CharDictionary.GetCharUnit(item);

                //判断是否有字符单元
                if (null == unit)
                {
                    //直接添加字符
                    builder.Append(item);
                }
                else
                {
                    //判断是否包含了拼音
                    bool hasPinYin = false;

                    //判断是否有拼音
                    if (unit.PinyinCount > 0)
                    {
                        //循环添加拼音
                        for (int index = unit.PinyinCount - 1; index >= 0; index--)
                        {
                            //获得拼音单元
                            PinyinUnit pinyin = g_PinyinDictionary.GetPinYinUnitByIndex(unit.PinyinIndexList[index]);

                            //判断是否有拼音
                            if (null == pinyin)
                            {
                                //如果没有拼音，则忽略
                                continue;
                            }

                            //添加拼音的首字母
                            builder.Append(pinyin.Pinyin[0]);

                            //设置有拼音
                            hasPinYin = true;

                            //终止循环
                            break;
                        }
                    }

                    //判断是否添加了拼音
                    if (!hasPinYin)
                    {
                        //直接添加字符
                        builder.Append(item);
                    }
                }
            }

            //返回结果
            return builder.ToString();
        }

        #endregion

        #region ---Field

        /// <summary>
        /// 字符字典
        /// </summary>
        private static CharDictionary g_CharDictionary;

        /// <summary>
        /// 拼音字典
        /// </summary>
        private static PinyinDictionary g_PinyinDictionary;

        #endregion
    }
}
